package com.sali.redis.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ConditionalOnProperty("spring.redis.host")
@Configuration
@Slf4j
public class RedissonConfiguration {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.connectionMinimumIdleSize:24}")
    private int connectionMinimumIdleSize;

    @Value("${spring.redis.connectionPoolSize:64}")
    private int connectionPoolSize;



    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        SingleServerConfig serverConfig = config.useSingleServer();
        serverConfig.setPingConnectionInterval(0);// 防止出现 redis 连接不上的问题
        serverConfig.setDnsMonitoringInterval(-1);
        serverConfig.setConnectionPoolSize(connectionPoolSize);
        serverConfig.setAddress("redis://" + host + ":" + port);
        if (StringUtils.isNotEmpty(password)) {
            serverConfig.setPassword(password);
        }
        return Redisson.create(config);
    }

}
